package com.wanmait.zaisousuo.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wanmait.zaisousuo.pojo.Article;
import com.wanmait.zaisousuo.pojo.ArticleCollect;
import com.wanmait.zaisousuo.pojo.Userinfo;
import com.wanmait.zaisousuo.service.ArticleCollectService;
import com.wanmait.zaisousuo.service.ArticleService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

@Component
public class ArticleCollectJob {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private ArticleService articleService;

    @Resource
    private ArticleCollectService articleCollectService;


    //每天0点更新数据库 文章收藏数
    @Scheduled(cron = "0 0 0 * * ? ")
    public void updateArticleCollectCount(){
        Set sets = redisTemplate.opsForSet().members("article:collect:set");//获取今天被收藏的文章set
        List<Article> articles = new LinkedList<>();
        Integer id;
        Integer count;
        for(Object o:sets){
            id = Integer.parseInt((String)o);//获取被收藏的文章的id
            count = Integer.parseInt((String) redisTemplate.opsForValue().get("article:"+id+":collect:count:"));//获取本篇文章收藏数
            Article article = new Article();
            article.setId(id);
            article.setCollectCount(count);
            articles.add(article);
        }
        articleService.updateCollectCount(articles);//数据库更新文章
    }


    //每30分钟添加一次数据
    @Scheduled(cron = "0 0/30 * * * ? ")
    public void updateCollects() throws NullPointerException,ClassCastException{
         List<ArticleCollect> articleCollects = new LinkedList<>();
         for(int i=0;i<20;i++){
             Integer articleId;
             Integer userinfoId;
             String s = (String)redisTemplate.opsForList().rightPop("article:collect:list");//从右弹出
             JSONObject jsonObject = JSONObject.parseObject(s);
             if(s!=null){
                 articleId = Integer.parseInt(jsonObject.get("articleId").toString());
                 userinfoId = Integer.parseInt(jsonObject.get("userinfoId").toString());
                 Article article = new Article();
                 Userinfo userinfo = new Userinfo();
                 article.setId(articleId);
                 userinfo.setId(userinfoId);
                 ArticleCollect articleCollect = new ArticleCollect();
                 articleCollect.setArticle(article);
                 articleCollect.setUserinfo(userinfo);
                 articleCollects.add(articleCollect);
             }else {
                 break;
             }
         }
         articleCollectService.insertCollects(articleCollects);
    }


    //每30分钟删除一次数据库里以前收藏过但是现在要取消收藏的数据。
    @Scheduled(cron = "0 0/30 * * * ? ")
    public void deleteCollects() throws NullPointerException,ClassCastException{
        List<ArticleCollect> articleCollects = new LinkedList<>();
        while(true){
            Integer articleId;
            Integer userinfoId;
            String s = (String)redisTemplate.opsForList().rightPop("article:delete:list");//从右弹出
            JSONObject jsonObject = JSONObject.parseObject(s);
            if(s!=null){
                articleId = Integer.parseInt(jsonObject.get("articleId").toString());
                userinfoId = Integer.parseInt(jsonObject.get("userinfoId").toString());
                Article article = new Article();
                Userinfo userinfo = new Userinfo();
                article.setId(articleId);
                userinfo.setId(userinfoId);
                ArticleCollect articleCollect = new ArticleCollect();
                articleCollect.setArticle(article);
                articleCollect.setUserinfo(userinfo);
                articleCollects.add(articleCollect);
            }else {
                break;
            }
        }
        articleCollectService.deleteCollects(articleCollects);
    }


}
